package com.audi.handler;

import java.util.ArrayList;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;

import com.audi.dto.DeviceItem;
import com.audi.dto.SmartTriggerWhenTriggerItem;

/**
 * @author PhongNT11
 * 
 */
public class DeviceHandler extends Handler {
	/**
	 * 
	 */
	protected DeviceHandler() {
		super();
	}

	/**
	 * processMessage
	 * 
	 * @param actionId
	 *            int
	 * @param data
	 *            String
	 * @return Message
	 * @see message.arg1 = 0 if return successful, message.arg1 = -1 if fail
	 */
	public Message process(int actionId, String data) {
		Message message = new Message();
		Bundle dataBundle = new Bundle();
		message.arg1 = 0;
		String key = String.valueOf(actionId);
		try {
			switch (actionId) {
			case addDevice:
				dataBundle.putInt(key, Integer.valueOf(data));
				break;
			case getDeviceListByGroup:
			case getAllDeviceList:
			case GET_DEVICE_LIST_BY_USER:
			case GET_DEVICE_LIST_BY_PLACE:
				dataBundle.putSerializable(key, json2ListDevice(data));
				break;
			case GET_DEVICE_BY_ID:
				dataBundle.putSerializable(key,
						convertJson2Obj(data, DeviceItem.class));
				break;
			case CHECK_DEVICE_EXIST:
				dataBundle.putBoolean(key, Boolean.valueOf(data));
				break;
			case deleteDevice:
				dataBundle.putInt(key, Integer.valueOf(data));
				break;
			case RENAME_DEVICE:
				dataBundle.putSerializable(key, json2ListDevice(data));
				break;
			case UPDATE_DEVICE:
				dataBundle.putInt(key, Integer.valueOf(data));
				break;
			case CHANGE_PLACE_OF_DEVICE:
				dataBundle.putInt(key, Integer.valueOf(data));
				break;
			case CHANGE_PLACE_OF_DEVICE_LIST:
				dataBundle.putInt(key, Integer.valueOf(data));
				break;
			case GET_LIST_DEVICE_FAVORITE:
				dataBundle.putSerializable(key, json2ListDevice(data));
				break;
			case UPDATE_FAVORITE_DEVICE:
				dataBundle.putInt(key, Integer.valueOf(data));
				break;
			default:
				break;
			}
		} catch (Exception e) {
			Log.e("Client.handler", "[DeviceHandler.java]: " + e.toString());
			message.arg1 = -1;
		}
		message.setData(dataBundle);
		return message;
	}

	public ArrayList<DeviceItem> json2ListDevice(String data) {
		ArrayList<DeviceItem> result = (ArrayList<DeviceItem>) convertJson2List(
				data, DeviceItem.class);
		return result;
	}
}
